﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillData();
    }
    void FillData()
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument doc = XDocument.Load(path);
        var student = from st in doc.Element("Students").Elements("Student")
                      select new
                      {
                          ID = st.Element("ID").Value,
                          Name = st.Element("Name").Value,
                          Address = st.Element("Address").Value
                      };

        GridView1.DataSource = student;
        GridView1.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (btnAdd.Text == "Add new")
        {
            btnAdd.Text = "Insert";
            Discible();
            btnAdd.Enabled = true;
            setnullTextbox();

        }
        else if (btnAdd.Text == "Insert")
        {
            if (txtID.Text.Trim() != null && txtName.Text.Trim() != null && txtAddress.Text.Trim() != null)
            {
                string path = Server.MapPath("~/FPT-Aptech2.xml");
                XDocument doc = XDocument.Load(path);
                var student = new XElement("Student");
                var id = new XElement("ID", txtID.Text);
                var name = new XElement("Name", txtName.Text);
                var address = new XElement("Address", txtAddress.Text);
                student.Add(id, name, address);
                doc.Root.Add(student);
                doc.Save(path);
                FillData();
                btnAdd.Text = "Add new";
                setnullTextbox();
                Enible();
            }
            else
            {
                setnullTextbox();
                txtID.Focus();
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            GridViewRow row = GridView1.SelectedRow;
            string Id = HttpUtility.HtmlDecode(row.Cells[1].Text);
            XDocument document = XDocument.Load(Server.MapPath("~/FPT-Aptech2.xml"));
            var deleteQuery = from r in document.Descendants("Student") where r.Element("ID").Value == Id select r;
            deleteQuery.First().Remove();
            document.Save(Server.MapPath("~/FPT-Aptech2.xml"));
            FillData();

        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            GridViewRow row = GridView1.SelectedRow;

            txtID.Text = HttpUtility.HtmlDecode(row.Cells[1].Text);
            txtName.Text = HttpUtility.HtmlDecode(row.Cells[2].Text);
            txtAddress.Text = HttpUtility.HtmlDecode(row.Cells[3].Text);
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            if (btnupdate.Text == "Update")
            {
                btnupdate.Text = "Insert";
                Discible();
                btnupdate.Enabled = true;
                txtID.Enabled = false;
            }
            else if (btnupdate.Text == "Insert")
            {
                if (txtID.Text.Trim() != null && txtName.Text.Trim() != null && txtAddress.Text.Trim() != null)
                {
                    GridViewRow row = GridView1.SelectedRow;
                    string Id = HttpUtility.HtmlDecode(row.Cells[1].Text);
                    XElement xEmp = XElement.Load(Server.MapPath("~/FPT-Aptech2.xml"));
                    var student = from st in xEmp.Elements("Student") where st.Element("ID").Value == Id select st;
                    student.First().Element("Name").Value = txtName.Text.Trim();
                    student.First().Element("Address").Value = txtAddress.Text.Trim();
                    xEmp.Save(Server.MapPath("~/FPT-Aptech2.xml"));
                    FillData();
                    Enible();
                    btnupdate.Text = "Update";
                    txtID.Enabled = true;
                    setnullTextbox();
                }
                else
                {
                    txtName.Text = "";
                    txtAddress.Text = "";
                    txtName.Focus();
                }
            }
        }
    }
    protected void Enible()
    {
        btnAdd.Enabled = true;
        btnDelete.Enabled = true;
        btnupdate.Enabled = true;
        GridView1.Enabled = true;

    }
    protected void Discible()
    {
        btnAdd.Enabled = false;
        btnDelete.Enabled = false;
        btnupdate.Enabled = false;
        GridView1.Enabled = false;

    }
    protected void setnullTextbox()
    {
        txtID.Text = "";
        txtName.Text = "";
        txtAddress.Text = "";


    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument doc = XDocument.Load(path);
        if (DropDownList1.SelectedItem.Text == "ID")
        {
            //var SearcdID = from student in xEmp.Elements("Students")
            //               where (string)student.Element("ID") == txtID.Text.Trim()
            //               select student;
            string Id = txtSearch.Text.Trim();
            var SearcdID = from student in doc.Element("Students").Elements("Student")
                           where student.Element("ID").Value == Id
                           select new
                           {
                               ID = student.Element("ID").Value,
                               Name = student.Element("Name").Value,
                               Address = student.Element("Address").Value
                           };

            GridView1.DataSource = SearcdID.ToList();
            GridView1.DataBind();

        }
        else if (DropDownList1.SelectedItem.Text == "Name")
        {
            string Id = txtSearch.Text.Trim();
            var SearcdID = from student in doc.Element("Students").Elements("Student")
                           where student.Element("Name").Value == Id
                           select new
                           {
                               ID = student.Element("ID").Value,
                               Name = student.Element("Name").Value,
                               Address = student.Element("Address").Value
                           };

            GridView1.DataSource = SearcdID.ToList();
            GridView1.DataBind();
        }
    }
}